package recursion;

public class EightQueen {
    private static final int max = 8;
    private static int count = 0;
    public static void main(String[] args) {

        int arr[] = new int[max];
        setQueen(0, arr);
        System.out.println(count);
    }

    private static void setQueen(int row, int arr[]) {
        if (row == max) {
            count++;
            print(arr);
        } else {
            for (int i = 0; i < max; i++) {
                if (!isConflict(row, i, arr)) {
                    arr[row] = i;
                    setQueen(row + 1, arr);
                }
            }
        }
    }

    private static void print(int arr[]) {
        for (int i = 0; i < max; i++) {
            System.out.print(arr[i] + "   ");
        }
        System.out.println();
    }

    private static boolean isConflict(int row, int assume, int arr[]) {
        for (int i = 0; i < row; i++) {
            if (assume == arr[i] || Math.abs(row - i) == Math.abs(assume - arr[i]))
                return true;
        }
        return false;
    }
}